<template>
	<view style="overflow-x: hidden;">
		<view :style="{height: screenTop+'px'}" style="background: #f1f1f1;"></view>
		<uni-nav-bar :title="navTitle" backgroundColor="#f1f1f1">
			<view slot="left">
				<uni-icons @click="backAction()" type="back" size="25"></uni-icons>
			</view>
		</uni-nav-bar>
		<template>
			<view v-if="list.length == 0 && isLoad" class="u-f-ajc u-f-c noData">
				<image src="https://statics.xmcsrv.net/weixin/userManage/noUserData.png"
					style="height: auto;width: 332rpx;" mode="widthFix"></image>
				<text>暂无数据</text>
			</view>
			<scroll-view v-else scroll-y="true" style="height: calc(100vh - 260rpx);" :paging-enabled="true"
				:refresher-enabled="true" :refresher-threshold="50" refresher-default-style="white"
				refresher-background="#f5f7fb" :enable-passive="true" :refresher-triggered="triggered"
				@refresherrefresh="onRefresh">
				<j-cell-group>
					<j-cell v-for="(item,index) in list" :key="index" :title="'用户'+item.Id+item.userType" :more="false">
						<view slot="tips">
							<uni-icons type="trash-filled" size="30" color="red" @click="deleteClick(item)"></uni-icons>
						</view>
					</j-cell>
				</j-cell-group>
			</scroll-view>
			<view class="find-food">
				<button class="radius-btn" type="operation" @tap="addUser('gengeral')">添加普通用户</button>
				<button class="addbutton radius-btn" type="operation" @tap="addUser('admin')">添加管理员用户</button>
			</view>
		</template>
	</view>
</template>

<script>
	const {
		turnTenToSixteen
	} = require('@/utils/util.js');
	const {
		handledoorLockTransparent
	} = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				device: {},
				userInfo: '',
				list: [],
				triggered: true,
				isLoad: false,
				navTitle: '管理员验证',
				password: '',
				screenTop: 0,
				action: 'Verify', //Verify 管理员验证，GetUser获取已添加的用户 AddUser添加新用户 DelUser删除用户
				addUserPswdModal: false,
				DoorUser: {
					Action: 'Verify',
					Type: 0
				}
			}
		},
		onLoad(opt) {
			console.log('userList-opt===>', opt)
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			this.userInfo = JSON.parse(opt.userInfo)
			console.log('userList===>userInfo', this.userInfo)
			this.navTitle = this.userInfo.title + '列表'
			this.password = opt.password
			const sys = uni.getSystemInfoSync();
			this.screenTop = sys.statusBarHeight
			this.DoorUser.Type = this.userInfo.type
		},
		onUnload() {
			this.setStatusEnd()
		},
		onShow() {
			this.DoorUser = {
				Action: 'GetUser',
				Err: 0,
				Type: this.userInfo.type
			}
			setTimeout(() => {
				this.getList()
			}, 500)
		},
		methods: {
			//添加用户
			addUser(type) {
				uni.showLoading()
				if (type == 'admin') {
					this.jumpToVerify(0, this.userInfo.type, type)
				} else {
					this.jumpToVerify(1, this.userInfo.type, type)
				}
			},
			// 跳转到验证页面
			jumpToVerify(authType, passwordType, type) {
				if (passwordType == 0) {
					uni.navigateTo({
						url: `./addUserPassword?authType=${authType}&userInfo=${JSON.stringify(this.userInfo)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
					})
				} else {
					uni.navigateTo({
						url: `./addUser?userInfo=${JSON.stringify(this.userInfo)}&admin=${type}&password=${this.password}&device=${encodeURIComponent(JSON.stringify(this.device))}`
					})
				}
			},
			//获取列表
			getList(type, callback, isAfterDelSuccess) {
				if (!isAfterDelSuccess) {
					uni.showLoading({
						title: type == 'Verify' ? '验证中' : type == 'DelUser' ? '正在删除' : '获取中'
					})
				}
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "DoorUser",
						DoorUser: this.DoorUser
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log(result, '门锁设置')
					uni.hideLoading()
					this.isLoad = true
					handledoorLockTransparent(result,(res,Err)=>{
						console.log(res,Err,'handledoorLockTransparent')
						if(Err == '0x00'){
							if (res.Users) {
								this.list = res.Users
								this.list = res.Users.map((item) => {
									return {
										...item,
										userType: item.Auth == 0 ? '(管理员)' : ''
									}
								})
							}
						}else{
							uni.showToast({
								title: getApp().globalData.errCodeMsg.wakeUpLockBoardCodeMsg[Err] || result.data
									.msg,
								icon: 'none',
								duration: 2000
							});
							this.setStatusEnd();
							setTimeout(() => {
								uni.navigateBack({})
							}, 1500)
						}
						if (typeof callback == 'function') {
							callback(res)
						}
					},true)
				},false);
			},
			//删除
			deleteClick(item) {
				uni.showModal({
					title: '提示',
					content: '确认要删除吗？',
					success: (res) => {
						if (res.confirm) {
							this.DoorUser = {
								Action: "DelUser",
								Id: item.Id,
								Type: this.userInfo.type
							}
							this.getList('DelUser', (res) => {
								uni.showToast({
									icon: 'success',
									title: '删除成功',
									duration: 1000
								})
								this.DoorUser = {
									Action: 'GetUser',
									Err: 0,
									Type: this.userInfo.type
								}
								this.getList(null, null, true)
							})
						}
					}
				})
			},
			onRefresh() {
				this.triggered = true
				this.getList()
				setTimeout(() => {
					this.triggered = false
				}, 1000)
			},
			//确认输入管理员密码
			confirmPass() {
				if (this.password.length < 6) {
					return
				}
				this.DoorUser = {
					Action: 'Verify',
					Pswd: this.password
				}
				this.getList('Verify', (res) => {
					this.DoorUser = {
						Action: 'GetUser',
						Err: 0,
						Type: this.userInfo.type
					}
					this.navTitle = this.userInfo.title
					this.getList()
				})
			},
			setStatusEnd() {
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "SyncDoorStatus",
						DoorStatus: {
							Status: 'End'
						}
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log('发送End', result);
					if (result.data.code == 2000) {
						uni.setStorageSync('StartFlag', {})
					}
				}, false);
			},
			backAction() {
				uni.navigateBack()
			}

		}
	}
</script>

<style lang="scss">
	.verify-code {
		.xt__verify-code {
			margin: auto;
		}

		.xt__input-ground {
			margin: auto;

			.xt__code-text {
				font-size: 40rpx !important;
			}
		}

		/deep/.xt__code-text {
			font-size: 40rpx !important;
		}

		/deep/.xt__box {
			width: 70rpx !important;
			height: 70rpx !important;
			background: #fff;
			border-radius: 10rpx;
			border: #eee;
			border-width: 1px !important;
			border-style: solid !important;
		}
	}

	.verify {
		margin-top: 150rpx;

		.img {
			width: 376rpx;
			height: 267.5rpx;
		}

		.verifyDesc {
			font-size: 26rpx;
		}
	}

	.code {
		margin-top: 80rpx;
		margin: 50rpx 11px;
	}

	.noData {
		// transform: translateY(50%);
		margin-top: 150rpx;

		image {
			width: 332rpx;
			height: auto;
		}

		text {
			margin-top: 40rpx;
			color: #000;
			font-size: 28rpx;
			text-align: center;
			width: 450rpx;
		}
	}

	.find-food {
		text-align: center;
		position: fixed;
		bottom: 50rpx;
		width: 100%;
		font-size: 28rpx;
	}

	.addbutton {
		margin-top: 10px;
	}
</style>